<template>
<div>
    <Button v-if="showBackBtn" class="goBack" type="primary" @click="$router.back()">
        <Icon type="ios-arrow-back"/>
        返回
    </Button>
    <Form  ref="formCustom" :model="formCustom"   :rules="ruleCustom"  :label-width="100">
        <h3>密接患者</h3>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-main">
                    <Row>
                        <Col span="7">
                            <Form-item label="姓名" prop="name">
                                <i-input type="text" v-model="formCustom.name" placeholder="请输入姓名"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="7">
                            <Form-item label="来源国家" prop="from_country">
                                <i-input type="text" v-model="formCustom.from_country" placeholder="请输入来源国家"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="7">
                            <Form-item label="证件类型" prop="id_type">
                                <Select  style="width:100%" v-model="formCustom.id_type"
                                         placeholder="请选择证件类型">
                                    <Option v-for="(item,index) in cardType" :key="index" :value="item.value">{{item.label}}</Option>
                                </Select>
                            </Form-item>
                        </Col>
                        <Col span="7">
                            <Form-item label="证件号码" prop="id_num">
                                <i-input type="text" v-model="formCustom.id_num" placeholder="请输入证件号码"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="7">
                            <Form-item label="联系电话" prop="contact">
                                <i-input type="text" v-model="formCustom.contact" placeholder="请输入联系电话"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="7">
                            <Form-item label="上报日期" prop="imp_date">
                                <DatePicker @on-change="dateChange" :options="options1" :value="formCustom.imp_date"
                                            format="yyyy-MM-dd" type="date" placeholder="请选择上报日期"></DatePicker>
                            </Form-item>
                        </Col>
                    </Row>
                </div>
            </div>
        </Card>
        <h3>密接信息</h3>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-main">
                    <Row>
                        <Col span="7">
                            <Form-item label="密接时间" prop="mj_time">
                                <DatePicker @on-change="startChange" :value="formCustom.mj_time"
                                            format="yyyy-MM-dd"
                                            type="date" placeholder="选择密接时间"></DatePicker>
                            </Form-item>
                        </Col>
                        <Col span="7">
                                <Form-item label="密接方式" prop="mj_type">
                                    <Select  style="width:100%" v-model="formCustom.mj_type"
                                             placeholder="请选择密接方式">
                                        <Option v-for="(item,index) in connectionType" :key="index" :value="item.value">{{item.label}}</Option>
                                    </Select>
                                </Form-item>
                        </Col>
                    </Row>
                </div>
            </div>
        </Card>
        <h3>人员现状</h3>
        <Card class="line-style">
            <div class="line-wrap">
                <div class="line-main">
                    <Row>
                        <Col span="20">
                            <Form-item label="密接人员现状" prop="mj_status">
                                <Input type="textarea" v-model="formCustom.mj_status" :rows="4" placeholder="请输入密接人员现状"></Input>
                            </Form-item>
                        </Col>
                    </Row>
                </div>
            </div>
        </Card>
        <h3>接触人员</h3>
        <Card class="line-style" v-for="(item,index) in formCustom.contact_list" :key="index">
            <div class="line-wrap">
                <div class="line-main">
                    <Row>
                        <Col span="6">
                            <Form-item label="接触者姓名" :prop="`contact_list[${index}].name`" :rules="[{ required: true, message: '请输入接触者姓名', trigger: 'blur' }]">
                                <i-input type="text" v-model="item.name" placeholder="请输入接触者姓名"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="6">
                            <Form-item label="证件号" :prop="`contact_list[${index}].id_num`" :rules="[{ required: true, message: '请输入证件号', trigger: 'blur' }]">
                                <i-input type="text" v-model="item.id_num" placeholder="请输入证件号"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="6">
                            <Form-item label="关系" prop="relationship" >
                                <i-input type="text" v-model="item.relationship" placeholder="请输入关系"></i-input>
                            </Form-item>
                        </Col>
                        <Col span="4" v-if="!showDetail">
                            <Button type="success" @click="addLine()">添加</Button>&nbsp;&nbsp;
                            <Button type="error" @click="delLine(index)">删除</Button>
                        </Col>
                    </Row>
                </div>
            </div>
        </Card>
    </Form>
    <Row justify="center" type="flex" style="margin-top: 10px" v-if="!showDetail">
        <Col>
            <Button type="primary" @click="validateForm('formCustom')">保存</Button>
        </Col>
    </Row>
        <Modal
                v-model="modal"
                title="确认操作"
                width="20"
                @on-ok="handleSubmit"
        >
            <div style="text-align:center">
                <p>是否确认保存!</p>
            </div>
        </Modal>
</div>
</template>

<script>
import { update, insert, getById, getCardType, getConnectionType } from '@/api/outbreak-management/overseas-to-shen/close-personnel'
export default {
  data () {
    return {
      options1: { // 不可选今天之后的日期
        disabledDate (date) {
          return date && date.valueOf() > Date.now()
        }
      },
      // 弹窗控制
      modal: false,
      // 控制保存按钮显隐
      showDetail: false,
      // 控制返回按钮显隐
      showBackBtn: false,
      formCustom: {
        id: '',
        type: 'MJ',
        name: '', // 姓名
        from_country: '', // 国家
        id_type: '', // 证件类型
        id_num: '', // 证件号
        contact: '', // 电话号
        mj_time: '', // 密接时间
        mj_type: '', // 密接方式
        mj_status: '', // 密接人员现状
        imp_date: '', // 上报日期
        // 接触人员信息
        contact_list: [{
          name: '',
          id_num: '',
          relationship: ''
        }]
      },
      // 证件类型接口
      cardType: {},
      // 密接方式接口
      connectionType: {},
      ruleCustom: {
        // 预留
        name: [
          { required: true, message: '姓名必填', trigger: 'blur' },
          { min: 0, max: 12, message: '姓名长度不能超过12', trigger: 'blur' }
        ],
        from_country: [
          { required: true, message: '来源国家必选', trigger: 'change' }
        ],
        id_type: [
          { required: true, message: '证件类型必选', trigger: 'change' }
        ],
        id_num: [
          { required: true, message: '证件号必填', trigger: 'blur' }
        ],
        contact: [
          { required: true, message: '联系电话必填', trigger: 'blur' }
        ],
        leave_time: [
          { required: true, message: '密接时间必选', trigger: 'blur' }
        ],
        mj_type: [
          { required: true, message: '密接方式必选', trigger: 'change' }
        ],
        remarks: [
          { required: true, message: '密接人员现状必填', trigger: 'blur' }
        ],
        imp_date: [
          { required: true, message: '上报日期必选', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    validateForm (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.modal = true
        } else {
          this.$Message.error('表单验证失败,请检查后再试!')
        }
      })
    },

    /**
         * 日期格式化
         */
    dateChange (date) {
      this.formCustom.imp_date = date
    },
    /**
         * 日期格式化
         */
    startChange (date, index) {
      this.formCustom.mj_time = date
    },
    handleSubmit () {
      let requestData = this.formCustom
      if (requestData.id) {
        update(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      } else {
        insert(requestData).then(({ data }) => {
          let { errcode } = data
          if (errcode === 0) {
            this.$Message.success('保存成功!')
          } else {
            this.$Message.error('保存失败!')
          }
        })
      }
    },
    isEditStatus () {
      if (this.$route.query && this.$route.query.id) {
        if (this.$route.query.showDetail) {
          this.showDetail = true
        }
        this.showBackBtn = true
        this.getEditData(this.$route.query.id)
      }
    },
    getEditData (id) {
      getById({ id }).then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.formCustom = res
          this.formCustom.imp_date = this.$common.moment(res.imp_date).format('YYYY-MM-DD')
          this.formCustom.mj_time = this.$common.moment(res.mj_time).format('YYYY-MM-DD')
        }
        console.log(data)
      })
    },
    addLine () {
      this.formCustom.contact_list.push(
        {
          name: '',
          id_num: '',
          relationship: ''
        })
    },
    delLine (index) {
      let len = this.formCustom.contact_list.length
      if (len === 1) {
        this.$Message.warning('只有一条数据不允许删除!')
        return
      }
      this.formCustom.contact_list.splice(index, 1)
    },
    // 调用证件类型接口
    getCardType () {
      getCardType().then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.cardType = res
        }
      }
      )
    },
    // 调用密接方式接口
    getConnectionType () {
      getConnectionType().then(({ data }) => {
        let { errcode, data: res } = data
        if (errcode === 0) {
          this.connectionType = res
        }
      }
      )
    }
  },
  created () {
    this.isEditStatus()
    // 调用证件类型接口
    this.getCardType()
    // 调用密接方式接口
    this.getConnectionType()
  },
  mounted () {
  }
}
</script>

<style scoped>
    h3{
        margin: 10px auto;
    }
</style>
